﻿<?php
/**
 * 定义 Controller_BoProduct 类
 *
 * @copyright Copyright (c) 2004 - 2009 iTeam
 * @author iTeam Monkey
 * @package XingYaoDianZi
 * @version 200912241027
 */

FLEA::loadClass('Controller_BoBase');
class Controller_BoFormula extends Controller_BoBase
{
    var $_modelClasses;
    var $_tableFormula;
	var $_tableUser;
    var $access;
    var $_helperFunction;
	var $dept_id;
	var $position_id;
	var $edu_id;
    var $dbo;
    
	//构造函数
    function Controller_BoFormula() {
        parent::Controller_BoBase();
        $this->access = USER_ADMIN;
        //用户角色权限控制
        if(!$this->checkDispatcher($this->access)){
            redirect(url('BoDashboard','ErrorIn'));
        }
        $this->_modelClasses =& FLEA::getSingleton('Model_Categories');
        $this->_tableFormula =& FLEA::getSingleton('Table_Formula');
		$this->_tableUser =& FLEA::getSingleton('Table_User');
		$this->dept_id = DEPARTMENT_CLASS;
		$this->position_id = POSITION_CLASS;
		$this->edu_id = EDUCATION_CLASS;
        $this->_helperFunction =& FLEA::getSingleton('Helper_Function');
        $this->dbo = &FLEA::getDBO();
    }
    
	//教育
    function actionEducation() {
        //查询当前分类
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
		$type=101;//类型为101的是学历规则
		$field = array();
		$field['type']=$type;
        $table =& $this->_tableFormula;
        $pager =& new FLEA_Helper_Pager($table, $page, 30, $field, 'department ASC,foreign_key ASC,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*",true);
        $this->_setBack();
//        dump($this->dbo->log);
        $listTitle = "学历规则";
		$listName = "学历";
        include(TPL_DIR . '/formulaList.php');
    }  
	//职位
    function actionPosition() {
        //查询当前分类
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
		$type=102;//类型为102的是职位规则
		$field = array();
		$field['type']=$type;
        $table =& $this->_tableFormula;
        $pager =& new FLEA_Helper_Pager($table, $page, 30, $field, 'department ASC,foreign_key ASC,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*",true);
        $this->_setBack();
//        dump($this->dbo->log);
        $listTitle = "职位规则";
		$listName = "职位";
        include(TPL_DIR . '/formulaList.php');
    }
	//特殊
    function actionSpecial() {
        //查询当前分类
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
		$type=999;//类型为999的是特殊规则
		$field = array();
		$field['type']=$type;
		if($_REQUEST['code']){
			$users = $this->_tableUser->findAll(array("code like '%".$_REQUEST['code']."%'"));
			if($users){
				$uidStr = 'foreign_key in(';
				foreach ($users as $userItem){
					$uidStr .= $userItem['uid'].',';
				}
				$uidStr = substr($uidStr, 0, strlen($uidStr)-1);
				$uidStr .= ')';
				$field[] = $uidStr;
			}
			else $field['foreign_key'] = 0;
		}
        $table =& $this->_tableFormula;
        $pager =& new FLEA_Helper_Pager($table, $page, 100, $field, 'department ASC,foreign_key ASC,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*",true);
        $sortArr = array('User','code');
        if($rowset)$rowset=$this->array_sort($rowset,"ASC");
        $this->_setBack();
//        dump($this->dbo->log);
        $listTitle = "特殊规则";
		$listName = "编号";
		$isCode = true;
		//var_dump($rowset);
        include(TPL_DIR . '/specialList.php');
    }
	//工龄工资
    function actionSeniority() {
        //查询当前分类
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
		$type=103;//类型为103的是工龄工资规则
		$field = array();
		$field['type']=$type;
        $table =& $this->_tableFormula;
        $pager =& new FLEA_Helper_Pager($table, $page, 30, $field, 'department ASC,foreign_key ASC,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*",true);
        $this->_setBack();
//        dump($this->dbo->log);
        $listTitle = "工龄工资";
		$isCode = true;
		//var_dump($rowset);
        include(TPL_DIR . '/seniorityList.php');
    }
	//晚班津贴
    function actionNight() {
        //查询当前分类
		$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
		$type=104;//类型为104的是晚班津贴规则
		$field = array();
		$field['type']=$type;
        $table =& $this->_tableFormula;
        $pager =& new FLEA_Helper_Pager($table, $page, 30, $field, 'department ASC,month ASC,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*",true);
        $this->_setBack();
//        dump($this->dbo->log);
        $listTitle = "晚班津贴";
		$isCode = true;
		//var_dump($rowset);
        include(TPL_DIR . '/nightList.php');
    }
     
    // 添加产品
    function actionCreate() {
        $table =& $this->_tableFormula;
        $article = $this->_prepareData($table->meta);
        $this->_editArticle($article);
    }
    
    //保存
    function actionSave() {
        __TRY();		
		if(!$_POST['fid']){
			$_POST['dateline'] = time();
			$field = array();
			if($_POST['type'])$field['type'] = $_POST['type'];
			if($_POST['foreign_key'])$field['foreign_key'] = $_POST['foreign_key'];
			if($_POST['month'])$field['month'] = $_POST['month'];
			if($_POST['department'])$field['department'] = $_POST['department'];
			$result = $this->_tableFormula->findAll($field);
		}
		if(!$result){
			$_POST['updated'] = time();		
        	$arid = $this->_tableFormula->save($_POST);
		}else{
			echo "<script>alert('记录已经存在，不要重复添加！');</script>";
			exit;
		}
//        dump($this->dbo->log);
        $ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
        	echo $ex->getMessage();
        	exit;
 	    	return $this->_editArticle($_POST, $ex->getMessage());
        }
		if($_POST['type']==101)redirect($this->_url('Education'));
		elseif($_POST['type']==102)redirect($this->_url('Position'));
		elseif($_POST['type']==999)redirect($this->_url('Special'));
		elseif($_POST['type']==104)redirect($this->_url('Night'));
		else redirect($this->_url('Seniority'));
    }

    //修改文章
    function actionEdit() {
        $article = $this->_tableFormula->findByField('fid',$_GET['id']);
        $this->_editArticle($article);
    }

	//显示文章信息编辑页面
    function _editArticle($article, $errorMessage = '') {
//    	var_dump($errorMessage);
    	$catid = $this->catid;
    	$table =& $this->_tableFormula;
        $pk = $table->primaryKey;
        $dept=$this->_modelClasses->getClass($this->dept_id);
        $deptclasses=$this->_modelClasses->getSubClasses($dept);
		$posi=$this->_modelClasses->getClass($this->position_id);
		$positions=$this->_modelClasses->getSubClasses($posi);
		$edu=$this->_modelClasses->getClass($this->edu_id);
		$education=$this->_modelClasses->getSubClasses($edu);
		$users = $this->_tableUser->findAll(null,'code asc');
		include(TPL_DIR . '/formulaEdit.php');
    }
    
    //删除资讯
    function actionRemove() {
    	$this->_tableFormula->removeByPkv($_GET['id'],false);        
        $this->_goBack();
	}
	//数组排序
	function array_sort($arr,$order){		
		foreach($arr as $key=>$value){
	         $temp[$value['User']['code']]= $value;
	    }
	    if($order=="ASC"){
	         ksort($temp);//升序排序临时组成的数组
		}
		if($order=="DESC"){
			ksort($temp);
			arsort($temp);//反转
		}
		return $temp;
	}
}
